import 'package:flutter/material.dart';
import '../model/post.dart';
import 'package:cached_network_image/cached_network_image.dart';
// class ListViewDemo extends StatefulWidget {
//   Widget _listItemBuilder(BuildContext context, int index){
//     return Container(
//       color: Colors.white,
//       margin: EdgeInsets.all(8),
//       child: Column(
//         children: <Widget>[
//           Image.network('https://hbimg.huabanimg.com/fa34196ae249feed3bea7ab51e874000659337ac52ef6-AI22Dq_fw658/format/webp'),
//           SizedBox(height: 16,),
//           Text(
//             posts[index].title,
//             style: Theme.of(context).textTheme.headline6,
//           ),
//           Text(
//             posts[index].author,
//             style: Theme.of(context).textTheme.subtitle1,
//           ),
//           SizedBox(height: 16,),
//         ],
//       ),
//     );
//   }
//
//   @override
//   Widget build(BuildContext context) {
//     // TODO: implement build
//     return ListView.builder(
//       itemCount: posts.length,
//       itemBuilder: _listItemBuilder,
//     );
//   }
// }

class ListViewDemo extends StatefulWidget {
  @override
  _ListViewDemoState createState() => _ListViewDemoState();
}

class _ListViewDemoState extends State<ListViewDemo> with AutomaticKeepAliveClientMixin {
  int a = 0;

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ListView.builder(
      itemCount: posts.length,
      itemBuilder: _listItemBuilder,
    );
  }

  Widget _listItemBuilder(BuildContext context, int index){
    return Container(
      color: Colors.white,
      margin: EdgeInsets.all(8),
      child: Column(
        children: <Widget>[
          CachedNetworkImage(
            imageUrl: "https://hbimg.huabanimg.com/fa34196ae249feed3bea7ab51e874000659337ac52ef6-AI22Dq_fw658/format/webp",
            placeholder: (context, url) => LinearProgressIndicator(),
            errorWidget: (context, url, error) => Icon(Icons.error),
          ),
          // Image.network('https://hbimg.huabanimg.com/fa34196ae249feed3bea7ab51e874000659337ac52ef6-AI22Dq_fw658/format/webp'),
          SizedBox(height: 16,),
          Text(
            posts[index].title,
            style: Theme.of(context).textTheme.headline6,
          ),
          Text(
            posts[index].author,
            style: Theme.of(context).textTheme.subtitle1,
          ),
          SizedBox(height: 16,),
        ],
      ),
    );
  }
  @override
  void initState() {
    super.initState();
  }
}